<template>
  <div>
    <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="form.username" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserInfoRequest, updateUserInfoRequest } from '@/api/employees'

export default {
  name: '',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名必填', trigger: 'change' }
        ],
        password: [
          { required: true, message: '密码必填', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfoRequest(this.$route.query?.id || '')
        // 业务成功以后，的处理
        // 页面数据的回填
        if (res.success) {
          // this.form.username = res.data.username
          // this.form.password = res.data.password
          // 因为更新的时候，需要很多个参数，我们不能把res.data里面的其他数据丢弃
          this.form = res.data
        }
      } catch (err) {
        console.log('getUserInfoRequest: ', err)
      }
    },
    async updateUserInfo() {
      try {
        const res = await updateUserInfoRequest(this.form)
        console.log(1)
        // 写完请求以后，await的后面，立即写 业务成功失败的代码
        if (res.success) {
          // 更新成功以后
          this.$message.success('更新用户信息成功')
          this.$router.back()
        } else {
          this.$message.error('更新用户信息失败')
        }
      } catch (err) {
        console.log('updateUserInfoRequest: ', err)
      }
    },
    onSubmit() {
      this.$refs.formRef.validate(val => {
        if (val) {
          // 校验成功以后 发请求
          this.updateUserInfo()
          console.log(2)
        }
      })
    }
  }
}
</script>
